package com.cloudcc.mobile.adapter;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import com.cloudcc.cloudframe.util.GsonUtil;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.ExtraConstant;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.ReceiverConstant;
import com.cloudcc.mobile.entity.beau.BeauInfoEntity;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.view.activity.EditorActivity;
import com.cloudcc.mobile.view.activity.MapsActivity;
import com.cloudcc.mobile.view.web.CloudWebViewActivity;
import com.cloudcc.mobile.view.web.WebSyncDefaultProxy;
import com.cloudcc.mobile.view.web.WebSyncDefaultProxy.MonitorParam;
import com.google.gson.JsonObject;
import com.litesuits.android.log.Log;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * 一个简单的ListViewAdapter适配器写法，内含跳转方法。
 * 
 * @author 
 *
 */
public class NewSelectAdapter extends BaseAdapter  {

	// 上下文
	private Context context;

	// List表
	private List<String> mList;
	private String mEntityId,mEntityNameID,jsonname;
	private BeauInfoEntity mBeauInfo;

	// 构造函数
	public NewSelectAdapter(Context context, List<String> list,String mEntityId,String mEntityNameID,BeauInfoEntity mBeauInfo,String jsonname) {
		this.context = context;
		this.mList = list;
		this.mEntityId = mEntityId;
		this.mEntityNameID = mEntityNameID;
		this.mBeauInfo = mBeauInfo;
		this.jsonname  =jsonname;
	}

	// List表单的总数
	public int getCount() {
		return mList.size();
	}

	// 位于position处的List表单的一项
	public Object getItem(int position) {
		return mList.get(position);
	}

	// List表单的各项的索引
	public long getItemId(int position) {
		return position;
	}

	// 最重要的获得视图内容方法
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewCache viewCache = null;

		if (convertView == null) {
			viewCache = new ViewCache();

			// LayoutInflater布局填充器，名副其实，见名知意。
			convertView = LayoutInflater.from(context).inflate(
					R.layout.new_select_list_item, null);

			// 根据布局并通过控件各自的ID找控件。
		
			viewCache.name = (TextView) convertView
					.findViewById(R.id.new_select_item_name);
			viewCache.imageView = (ImageView) convertView
					.findViewById(R.id.huodong_tupian);

			// 为每项的TextView加个Tag
			viewCache.name.setTag(position);

			// 为convertView添加标记
			convertView.setTag(viewCache);

		} else {

			// 根据标记找到viewCache，达到缓存的目的（软引用）
			viewCache = (ViewCache) convertView.getTag();
		}

		// 给TextView设置内容
		viewCache.name.setText(mList.get(position));
		final String name = mList.get(position);
		
		//给imageview设置图片
		
		 try {
			 int bg_id;
			 Class drawable = R.drawable.class;
			String bgid = "tu" + ((position) % 5);
				Field field = drawable.getField(bgid);
				 bg_id = field.getInt(field.getName());
		
		 
		if((mList.get(position).contains("邮件"))||(mList.get(position).contains("email"))){
			viewCache.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.shijianyoujian));
		}else if((mList.get(position).contains("电话"))||(mList.get(position).contains("phone"))){
			viewCache.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.shijiandianhua));
		}else if((mList.get(position).contains("签到"))||(mList.get(position).contains("Check in"))){
			viewCache.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.qiandao));
		}else if((mList.get(position).contains("任务"))||(mList.get(position).contains("task"))){
			viewCache.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.renwu));
		}else{
			viewCache.imageView.setImageBitmap(null);
			viewCache.imageView.setBackgroundResource(bg_id);
		}
		 } catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		// 添加监听事件
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 Activity activity = (Activity)context;
		            activity.finish();
				if ("签到".equals(mList.get(position))) {
					Intent intent1 = new Intent(context, MapsActivity.class);
					intent1.putExtra("beauinfo", "beau");
					intent1.putExtra("name", mList.get(position));
					intent1.putExtra("id", mEntityId);
					context.startActivity(intent1);
				} else if ("任务".equals(name)) {
					String id = mEntityId;
					// 潜在客户 004201700F462EEAr2Db 联系人 004201700F462EEAr2Db
					String url = "";
					if ((StringUtils.equalsIgnoreCase(mEntityNameID, "lead"))
							|| (StringUtils.equalsIgnoreCase(mEntityNameID,
									"contact"))) {// 潜在客户 联系人
						String whoid = mEntityId;
						String whoobj = mEntityId.substring(0, 3);
						url = UrlManager.getNewTaskOrSchedule_x(true, whoid,
								whoobj);
					} else {
						String relateid = mEntityId;
						String relateobj = mEntityId.substring(0, 3);
						url = UrlManager.getNewTaskOrSchedule_y(true, relateid,
								relateobj);
					}
					Log.d("request", "新建任务的" + url);
					MonitorParam monitorParam = new MonitorParam(
							new String[] { "m=save" },
							WebSyncDefaultProxy.STEP_START,
							new String[] { "m=query" }, true,
							WebSyncDefaultProxy.STEP_OVERRIDEURL);
					Intent intent = new Intent(context,
							CloudWebViewActivity.class);
					intent.putExtra(
							"callback",
							WebSyncDefaultProxy
									.creatDefaultProxy(
											ReceiverConstant.RECEIVE_REFRESH_SCHEDULE)
									.addMonitorParam(monitorParam)
									.addHomeMonitor());
					intent.putExtra(ExtraConstant.EXTRA_URL, url);
					context.startActivity(intent);
				} else {
					String relevantName;
					try {
						
						relevantName = name + "【"
								+ jsonname
								+ "】";
						AppContext.relevantName =jsonname;
					} catch (Exception e) {
						relevantName = name;
						e.printStackTrace();
					}
					String reuevantType = name;
					String key = "1";
					Intent intent = new Intent(context, EditorActivity.class);
					intent.putExtra("urlId", mEntityId);
					intent.putExtra("key", key);
					intent.putExtra("CustomerActivity", relevantName);
					intent.putExtra("reuevantType", reuevantType);
					intent.putExtra("name", name);
					intent.putExtra("from", "web");
					context.startActivity(intent);
				}
			}
		});
		

		return convertView;
	}

	// 根据传进来的action跳转到不同的Activity
	private void startActivityMethod(String action) {
		Intent intent = new Intent();

		// 下面2行都可以跳转，不过跳转的方式不一样。
		// intent.setClassName("com.testlistview","com.testlistview.TestActivity");
		intent.setAction(action);
		context.startActivity(intent);
	}

	// 辅助缓存类
	class ViewCache {
		TextView  name;
		ImageView imageView;
	}

}
